<html>
<head><meta charset="utf-8"><title>how to optimization my code with mutable hashmap borrow. · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/how.20to.20optimization.20my.20code.20with.20mutable.20hashmap.20borrow.2E.html">how to optimization my code with mutable hashmap borrow.</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="218067188"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/how%20to%20optimization%20my%20code%20with%20mutable%20hashmap%20borrow./near/218067188" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Phodal Huang <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/how.20to.20optimization.20my.20code.20with.20mutable.20hashmap.20borrow.2E.html#218067188">(Nov 27 2020 at 08:37)</a>:</h4>
<p>Hi, i working in a textmate rules parser with rust. When I finish it, I found it's very very slowly. And I figure out those code with <code>clone</code> is the reason.  The <code>rule_id2desc</code> is a hashmap, I need to got a rule from it, and use it to build caches. But it cause <code>second mutable borrow occurs here</code>, I try to resolve it different ways but not working.</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="w">    </span><span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">compile_rule</span><span class="p">(</span><span class="w"></span>
<span class="w">        </span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="bp">self</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="n">stack</span>: <span class="kp">&amp;</span><span class="nc">mut</span><span class="w"> </span><span class="n">StackElement</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="n">allow_a</span>: <span class="kt">bool</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="n">allow_g</span>: <span class="kt">bool</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">CompiledRule</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="n">rule_id</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">stack</span><span class="p">.</span><span class="n">rule_id</span><span class="p">;</span><span class="w"></span>
<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">rule</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="bp">self</span><span class="p">.</span><span class="n">rule_id2desc</span><span class="p">.</span><span class="n">get_mut</span><span class="p">(</span><span class="o">&amp;</span><span class="n">rule_id</span><span class="p">).</span><span class="n">unwrap</span><span class="p">().</span><span class="n">clone</span><span class="p">();</span><span class="w"></span>
<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="n">rule_scanner</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">rule</span><span class="p">.</span><span class="n">compile</span><span class="p">(</span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="bp">self</span><span class="p">.</span><span class="n">rule_id2desc</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">stack</span><span class="p">.</span><span class="n">end_rule</span><span class="p">,</span><span class="w"> </span><span class="n">allow_a</span><span class="p">,</span><span class="w"> </span><span class="n">allow_g</span><span class="p">);</span><span class="w"></span>

<span class="w">        </span><span class="bp">self</span><span class="p">.</span><span class="n">rule_id2desc</span><span class="p">.</span><span class="n">insert</span><span class="p">(</span><span class="n">rule_id</span><span class="p">,</span><span class="w"> </span><span class="n">rule</span><span class="p">);</span><span class="w"></span>

<span class="w">        </span><span class="n">rule_scanner</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>How can I make a changable hashmap in this code? Has anyone have a suggestion about this?</p>



<a name="218069780"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/how%20to%20optimization%20my%20code%20with%20mutable%20hashmap%20borrow./near/218069780" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/how.20to.20optimization.20my.20code.20with.20mutable.20hashmap.20borrow.2E.html#218069780">(Nov 27 2020 at 09:12)</a>:</h4>
<p>Try moving <code>self.register_rule</code> before <code>rule.compile</code> and then get a reference to the <code>rule</code> from the hashmap just before <code>rule.compile</code>.</p>



<a name="218072068"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/how%20to%20optimization%20my%20code%20with%20mutable%20hashmap%20borrow./near/218072068" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Phodal Huang <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/how.20to.20optimization.20my.20code.20with.20mutable.20hashmap.20borrow.2E.html#218072068">(Nov 27 2020 at 09:38)</a>:</h4>
<p>Sorry, I forgot to paste origin code, the rule is mut, so... I need to get mut rule, to update self in Hashmap.. Does <code>Rc&lt;RefCell</code> will works ?</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>